/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package compiledriver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;


/**
 *
 * @author Mark
 * <p> Extends Operation and
 * Represents a <code>Procedure</code> that implements other operations</p>
 * @throws IllegalState Exception if Operation does not exist or if it uses an 
 * operation with the wrong number of arguments.</p>
 * 
 * 
 */
public class Procedure extends Operation {
    Procedure(ListIterator<String> intext, HashMap<String, Definition> defHash,
            HashMap<String, Operation> opHash) {
        super(intext, defHash);
        if(intext.next().equals("begin"))
        {
            boolean continueLoop = true;
            while(continueLoop)
            {
                String nxt = intext.next();
                if (nxt.equals("end"))
                {
                    continueLoop = false;
                }
                else
                {
                    intext.previous();
                    Call callAdd = new Call(intext);
                    if (!opHash.containsKey(callAdd.getId()))
                    {
                        throw new IllegalStateException("Did not find " 
                                + callAdd.getId() + " in the Operations listed");
                    }
                    else if (opHash.get(callAdd.getId()).argList.size() 
                            != callAdd.argList.size())
                    {
                        throw new IllegalStateException("The number of arguments "
                                + "given for " + callAdd.getId() + ": " 
                                + callAdd.argList.size() + " does not match the " 
                                + "number of arguments for " + opHash.get(callAdd.getId())
                                + ": " + opHash.get(callAdd.getId()).argList.size());
                    }
                    callList.add(callAdd);
                }
            }
        }
    }

    protected ArrayList<Call> callList = new ArrayList<Call>();
    //does this need to be here?
    protected ArrayList<Operation> opList = new ArrayList<Operation>();

    
    /**
     * <p>Returns the size of the internal callList</p>
     * @return size of the callList
     */
    public int callCount()
    {
        return callList.size();
    }

    


    /**
     * <p>Returns a shallow copy of the call list, just for basic safety</p>
     * @return <code>ArrayList</code> of shallow copied <code>Call</code>s
     */
    public ArrayList<Call> getCallList()
    {
        return new ArrayList<Call>(callList);
    }

    @Override
    public String toString()
    {
        String retval = super.toString();
        for (Call c : callList)
        {
            retval += "\n" + c.toString();
        }
        return retval;
    }

}
